package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.Reason;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {




    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }
@Autowired
private IWorkerCertificationAuditService workerCertificationAuditService;
    @Autowired
    private IServeProviderService serveProviderService;
    /**
     * 服务端提交认证申请
     *
     * @param dto
     */
    @Override
    public void serveCommit(WorkerCertificationAuditAddReqDTO dto) {
        WorkerCertification workerCertification = BeanUtil.toBean(dto, WorkerCertification.class);
        workerCertification.setId(dto.getServeProviderId());
        workerCertification.setCertificationStatus(CertificationStatusEnum.INIT.getStatus());
        workerCertification.setCertificationTime(LocalDateTime.now());
        boolean result = this.save(workerCertification);
        if (!result){
            throw new RuntimeException("服务端提交认证申请失败");
        }

        WorkerCertificationAudit workerCertificationAudit = new WorkerCertificationAudit();
        workerCertificationAudit.setServeProviderId(workerCertification.getId());
        BeanUtils.copyProperties(workerCertification, workerCertificationAudit);
        workerCertificationAudit.setAuditStatus(0);
        workerCertificationAudit.setId(null);
        workerCertificationAudit.setCertificationStatus(1);
        workerCertificationAuditService.save(workerCertificationAudit);

    }

    @Override
    public Reason rejectReason() {
        Map<String, String> rejectReason = new HashMap<>();
        Long userId = UserContext.currentUserId();
        String reason = workerCertificationAuditService.lambdaQuery().eq(WorkerCertificationAudit::getServeProviderId, userId).eq(WorkerCertificationAudit::getAuditStatus,1).one().getRejectReason();
        Reason reason1 = new Reason();
        reason1.setRejectReason(reason);
        return reason1;
    }
}
